package com.xln.furns.dao.impl;

import com.xln.furns.dao.FurnDAO;
import com.xln.furns.entity.Furn;
import com.xln.furns.entity.Page;

import java.util.List;

public class FurnDAOimpl extends BasicDAO<Furn> implements FurnDAO {
    @Override
    public List<Furn> queryFurn() {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` `imgpath` FROM furn;";

        List list = queryMulti(sql,Furn.class);

        return list;
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)" +
                "VALUES(NULL , ? , ? , ? , ? , ? , ?)";

        return update(sql,furn.getName(),furn.getMaker(),furn.getPrice(),furn.getSales(),furn.getStock(),furn.getImgpath());
    }

    @Override
    public int deleteFurn(int id) {
        String sql = "DELETE FROM `furn` WHERE `id` = ?";

        return update(sql,id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` imgpath FROM `furn` WHERE `id` = ?";
        return querySingle(sql,Furn.class,id);
    }

    @Override
    public int update(Furn furn) {
        String sql = "UPDATE `furn` SET `name`=?,`maker`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? WHERE `id`=?";

        return update(sql,furn.getName(),furn.getMaker(),furn.getPrice(),furn.getSales(),furn.getStock(),furn.getImgpath(),furn.getId());
    }

    @Override
    public int getTotalRow() {
        String sql = "SELECT COUNT(*) FROM `furn`";

        return ((Number) queryScalar(sql)).intValue();
    }

    @Override
    public List<Furn> getPageItem(Integer begin, Integer pageSize) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` " +
                "imgpath FROM `furn` LIMIT ?,?";
        List<Furn> list = queryMulti(sql,Furn.class,begin,pageSize);
        return list;
    }

    @Override
    public int getTotalRowByname(String name) {
        String sql = "SELECT COUNT(*) FROM `furn` WHERE `name` LIKE ?";

        return ((Number) queryScalar(sql,"%"+name+"%")).intValue();
    }

    @Override
    public List<Furn> getPageItemsByname(int begin,int pageSize,String name) {
        String sql = "SELECT `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` " +
                "imgpath FROM `furn` WHERE `name` LIKE ? LIMIT ?,?";


        return queryMulti(sql,Furn.class,"%"+name+"%",begin,pageSize);
    }
}
